<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>


    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
</head>
<body>
<div id="app">

    <el-menu
            :default-active="activeIndex2"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">
        <el-menu-item index="1"><a href="http://localhost:8080/vue-demo/showAllGoods2.html">所有商品</a></el-menu-item>
        <el-submenu index="2">
            <template slot="title">我的工作台</template>
            <el-menu-item index="2-1" @click="dialogVisible = true">修改个人信息</el-menu-item>
            <el-menu-item index="2-2">与商户聊天</el-menu-item>
            <el-menu-item index="2-3" @click="dialogVisible10 = true">申请权限为商户端</el-menu-item>
        </el-submenu>
        <el-menu-item index="3" ><a href="http://localhost:8080/vue-demo/manageAddress2.html" target="_blank">管理地址</a></el-menu-item>
        <el-menu-item index="4"><a href="http://localhost:8080/vue-demo/showMyGouwucar2.html" target="_blank">我的购物车</a></el-menu-item>
        <el-menu-item index="5"><a href="http://localhost:8080/vue-demo/addPrice2.html" target="_blank">积分充值</a></el-menu-item>
        <el-submenu index="6">
            <template slot="title">支付订单管理</template>
            <el-menu-item index="6-1"><a href="http://localhost:8080/vue-demo/showAllOrderForm2.html" >所有订单</a></el-menu-item>
            <el-menu-item index="6-2"><a href="http://localhost:8080/vue-demo/showFalseOrderForm2.html" >未付款订单</a></el-menu-item>
            <el-menu-item index="6-3"><a href="http://localhost:8080/vue-demo/showSuccessOrderForm2.html" >已支付订单</a></el-menu-item>
        </el-submenu>
        <el-menu-item index="7" @click="signIn">每日签到</el-menu-item>
        <el-menu-item index="8" @click="dialogVisible11 = true">退出登录</el-menu-item>
    </el-menu>
    <!--按钮-->
    <el-row>
        <el-button type="danger" @click="dialogVisible = true">新增</el-button>
    </el-row>
    <!--添加数据的对话框表单-->
    <el-dialog
            title="退出登录"
            :visible.sync="dialogVisible11"
            width="30%">

        <el-form status-icon :rules="rules" ref="form" :model="user" label-width="80px">
            <el-form-item>
                <el-button type="primary" @click="cancelLogin">确认要退出登录吗</el-button>
                <el-button @click="dialogVisible11 = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!--添加数据的对话框表单-->
    <el-dialog
            title="警告"
            :visible.sync="dialogVisible10"
            width="30%">

        <el-form status-icon :rules="rules" ref="form" :model="user" label-width="80px">
            <el-form-item>
                <el-button type="primary" @click="changeUserStatue">确认要转变成商户</el-button>
                <el-button @click="dialogVisible10 = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!--添加数据的对话框表单-->
    <el-dialog
            title="新增地址"
            :visible.sync="dialogVisible"
            width="30%">
        <el-form status-icon :rules="rules" ref="form" :model="address" label-width="80px" >
            <el-form-item label="地址" prop="userAddress">
                <el-input v-model="address.userAddress" placeholder="请输入地址" :maxlength="12"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addAddress">立即创建</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>
    <!--添加数据的对话框表单-->
    <el-dialog
            title="删除地址"
            :visible.sync="dialogVisibleThree"
            width="30%">

        <el-form status-icon :rules="rules" ref="form" :model="address" label-width="80px" >
            <el-form-item>
                <el-button type="primary" @click="deleteAddress2(address.addressID)">确认删除</el-button>
                <el-button @click="dialogVisibleThree = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!--添加数据的对话框表单-->
    <el-dialog
            title="修改地址"
            :visible.sync="dialogVisibleFour"
            width="30%">
        <el-form status-icon :rules="rules" ref="form" :model="address" label-width="80px" >
            <el-form-item label="地址" prop="userAddress">
                <el-input v-model="address.userAddress" placeholder="请输入地址" :maxlength="12"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="modifyAddress2(address.addressID)">立即修改</el-button>
                <el-button @click="dialogVisibleFour = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>


    <el-tag>默认地址</el-tag>
    <el-input v-model="morenAddress.userAddress" readonly>默认地址</el-input>
    <!--表格-->
    <template>
        <el-table
                :data="AddressTableData"
                style="width: 100%"

                :row-class-name="tableRowClassName">
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="userAddress"
                    label="我的地址"
                    align="center">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <!-- 在这里，你可以使用scope.$index来获取当前行的索引值 -->
                    <el-button type="primary" @click="modifyMorenAddress(scope.$index)">设置为默认地址</el-button>
                    <el-button type="primary" @click="modifyAddress(scope.$index)">修改</el-button>
                    <el-button type="danger" @click="deleteAddress(scope.$index)">删除</el-button>
                </template>
            </el-table-column>

        </el-table>
    </template>
</div>


<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script src="js/axios-0.18.0.js"></script>

<script>
    new Vue({
        el:"#app",

        mounted(){
            //当页面加载完成之后，发布异步请求获取数据

            var _this = this;
            axios({
                method:"get",
                url:"http://localhost:8080/vue-demo/modifyAddress"
            }).then(function (resp) {
                _this.AddressTableData = resp.data;
            })

            axios({
                method:"get",
                url:"http://localhost:8080/vue-demo/modifyAddress2"
            }).then(function (resp) {
                _this.morenAddress = resp.data;
            })


        },
        methods: {
            handleSelect(key, keyPath) {
                console.log(key, keyPath);
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            signIn(){
                //this.goods = this.tableData[index];
                var _this = this;
                //发送请求
                axios({
                    method : "post",
                    url:"http://localhost:8080/vue-demo/signIn",
                }).then(function (resp) {
                    if (resp.data === "success") {
                        alert("今日签到成功，已为您增加2积分")
                    }
                    else if(resp.data === "error"){
                        alert("今日已签到")
                    }
                })
            },
            addAddress() {
                //console.log(this.AddressTableData);
                var _this = this;
                //发送请求
                axios({
                    method : "post",
                    url:"http://localhost:8080/vue-demo/addAddress",
                    data:_this.address
                }).then(function (resp) {

                    if (resp.data === "success") {
                        alert("新增地址成功")
                        _this.dialogVisible =  false
                    }
                })
            },
            changeUserStatue(){
                //this.goods = this.tableData[index];
                var _this = this;
                //发送请求
                axios({
                    method : "post",
                    url:"http://localhost:8080/vue-demo/changeUserStatue",
                }).then(function (resp) {
                    if (resp.data === "success") {
                        alert("您已经成功发送请求")
                        _this.dialogVisible10 = false;
                    }
                    else if(resp.data === "error"){
                        alert("您已经发出请求，请等待管理员审批")
                    }
                })
            },
            cancelLogin(){
                //this.goods = this.tableData[index];
                var _this = this;
                //发送请求
                axios({
                    method : "post",
                    url:"http://localhost:8080/vue-demo/cancelLogin",
                }).then(function (resp) {
                    if (resp.data === "success") {
                        alert("您已经退出登录")
                        window.location.href = "http://localhost:8080/vue-demo/hello.html";
                    }
                })
            },
            modifyAddress2(addressID) {
                //console.log(addressID);
                var _this = this;
                //发送请求
                axios({
                    method : "post",
                    url:"http://localhost:8080/vue-demo/modifyAddress3",
                    data:_this.address
                }).then(function (resp) {

                    if (resp.data === "success") {
                        alert("修改地址成功")
                        _this.dialogVisibleFour =  false
                    }
                })
            },
            modifyAddress(index) {
                //console.log(index);
                //console.log(this.tableData[index].goodsID);
                this.address.addressID = this.AddressTableData[index].addressID;
                this.address.userAddress = this.AddressTableData[index].userAddress;
                this.dialogVisibleFour=  true;
            },
            modifyMorenAddress(index) {
                //console.log(index);
                //console.log(this.tableData[index].goodsID);
                this.address.addressID = this.AddressTableData[index].addressID;
                this.address.userAddress = this.AddressTableData[index].userAddress;
                var _this = this;
                //发送请求
                axios({
                    method : "post",
                    url:"http://localhost:8080/vue-demo/modifyAddress4",
                    data:_this.address
                }).then(function (resp) {

                    if (resp.data === "success") {
                        alert("设置默认地址成功")
                    }
                })
            },
            deleteAddress2(addressID) {
                //console.log(addressID);
                var _this = this;
                //发送请求
                axios({
                    method : "post",
                    url:"http://localhost:8080/vue-demo/deleteAddress",
                    data:addressID
                }).then(function (resp) {

                    if (resp.data === "success") {
                        alert("删除地址成功")
                        _this.dialogVisibleThree =  false
                    }
                })
            },
            deleteAddress(index) {
                //console.log(index);
                //console.log(this.AddressTableData[index].addressID);
                this.address.addressID = this.AddressTableData[index].addressID;
                this.address.userAddress = this.AddressTableData[index].userAddress;
                this.dialogVisibleThree =  true;
            },

        },
        data() {
            var validateAddress = (rule, value, callback) => {
                if (value === '') {
                    return callback(new Error('请输入地址'));
                } else {
                    if(/\s/.test(value)){
                        //如果存在空格
                        return callback(new Error('地址不可以有空格'));
                    }
                    else {
                        callback();
                    }
                    //callback();
                }
            }
            return {
                activeIndex2: '1',
                currentPage: 4,
                //搜索表单数据
                AddressTableData: [{
                    userAddress: '',
                    userID: '',
                    addressID: '',
                }],
                address: {
                    userAddress: '',
                    userID: '',
                    addressID: ''
                },
                morenAddress:{
                    userAddress: '',
                    userID: '',
                    addressID: ''
                },
                rules: {
                    userAddress:[
                        {validator:validateAddress, trigger: 'blur' }
                    ],
                },
                //展开对话框
                dialogVisible: false,
                dialogVisibleThree: false,
                dialogVisibleFour: false,
                dialogVisible10: false,
                dialogVisible11: false,
            }
        }
    })
</script>

</body>
</html>